// recognize.js
Page({
  data: {
    imagePath: '',
    recognizing: false,
    recognitionResults: null,
    recognitionTypes: [
      { id: 'general', name: '通用物体识别' },
      { id: 'ocr', name: '文字识别' },
      { id: 'face', name: '人脸识别' },
      { id: 'landmark', name: '地标识别' }
    ],
    selectedType: 'general'
  },

  // 计算百分比分数
  calculateScore(score) {
    return Math.round(score * 100);
  },

  onLoad() {
    console.log('图片识别页面加载');
  },

  // 选择图片
  chooseImage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        
        this.setData({
          imagePath: tempFilePath,
          recognitionResults: null
        });
        
        console.log('选择的图片：', tempFilePath);
      }
    });
  },

  // 选择识别类型
  selectRecognitionType(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      selectedType: type,
      recognitionResults: null
    });
  },

  // 开始识别
  startRecognition() {
    if (!this.data.imagePath) {
      wx.showToast({
        title: '请先选择图片',
        icon: 'none'
      });
      return;
    }

    this.setData({
      recognizing: true,
      recognitionResults: null
    });

    // 根据选择的识别类型调用不同的模拟识别方法
    switch (this.data.selectedType) {
      case 'general':
        this.simulateGeneralRecognition();
        break;
      case 'ocr':
        this.simulateOcrRecognition();
        break;
      case 'face':
        this.simulateFaceRecognition();
        break;
      case 'landmark':
        this.simulateLandmarkRecognition();
        break;
      default:
        this.simulateGeneralRecognition();
    }
  },

  // 模拟通用物体识别
  simulateGeneralRecognition() {
    // 实际项目中应调用真实的识别API
    setTimeout(() => {
      const results = {
        type: 'general',
        items: [
          { name: '猫', score: 0.92 },
          { name: '宠物', score: 0.88 },
          { name: '哺乳动物', score: 0.85 },
          { name: '家猫', score: 0.82 },
          { name: '小猫', score: 0.78 }
        ]
      };
      
      this.setData({
        recognitionResults: results,
        recognizing: false
      });
      
      wx.showToast({
        title: '识别完成',
        icon: 'success'
      });
    }, 2000);
  },

  // 模拟文字识别
  simulateOcrRecognition() {
    // 实际项目中应调用真实的OCR API
    setTimeout(() => {
      const results = {
        type: 'ocr',
        text: '微信小程序\n图片识别示例\n这是一段示例文字\n用于演示OCR功能',
        words: [
          { text: '微信小程序', confidence: 0.98 },
          { text: '图片识别示例', confidence: 0.96 },
          { text: '这是一段示例文字', confidence: 0.95 },
          { text: '用于演示OCR功能', confidence: 0.94 }
        ]
      };
      
      this.setData({
        recognitionResults: results,
        recognizing: false
      });
      
      wx.showToast({
        title: '识别完成',
        icon: 'success'
      });
    }, 2000);
  },

  // 模拟人脸识别
  simulateFaceRecognition() {
    // 实际项目中应调用真实的人脸识别API
    setTimeout(() => {
      const results = {
        type: 'face',
        faces: [
          {
            age: 25,
            gender: '女',
            expression: '微笑',
            glasses: false,
            beauty: 85
          }
        ]
      };
      
      this.setData({
        recognitionResults: results,
        recognizing: false
      });
      
      wx.showToast({
        title: '识别完成',
        icon: 'success'
      });
    }, 2000);
  },

  // 模拟地标识别
  simulateLandmarkRecognition() {
    // 实际项目中应调用真实的地标识别API
    setTimeout(() => {
      const results = {
        type: 'landmark',
        landmarks: [
          { name: '故宫', confidence: 0.95, location: '北京市' },
          { name: '天安门', confidence: 0.85, location: '北京市' }
        ]
      };
      
      this.setData({
        recognitionResults: results,
        recognizing: false
      });
      
      wx.showToast({
        title: '识别完成',
        icon: 'success'
      });
    }, 2000);
  },

  // 复制识别结果
  copyResults() {
    if (!this.data.recognitionResults) {
      return;
    }
    
    let textToCopy = '';
    
    switch (this.data.recognitionResults.type) {
      case 'general':
        textToCopy = this.data.recognitionResults.items.map(item => `${item.name}: ${(item.score * 100).toFixed(0)}%`).join('\n');
        break;
      case 'ocr':
        textToCopy = this.data.recognitionResults.text;
        break;
      case 'face':
        const face = this.data.recognitionResults.faces[0];
        textToCopy = `年龄: ${face.age}\n性别: ${face.gender}\n表情: ${face.expression}\n颜值: ${face.beauty}`;
        break;
      case 'landmark':
        textToCopy = this.data.recognitionResults.landmarks.map(landmark => `${landmark.name}: ${landmark.location}`).join('\n');
        break;
    }
    
    wx.setClipboardData({
      data: textToCopy,
      success: () => {
        wx.showToast({
          title: '已复制到剪贴板',
          icon: 'success'
        });
      }
    });
  }
});
